package com.turnengine.client.local.upkeep.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.upkeep.bean.IUpkeepCondition;
import com.turnengine.client.local.upkeep.bean.UpkeepConditionSerializer;
import com.turnengine.client.local.upkeep.enums.UpkeepConditionType;
import java.io.IOException;

/**
 * The Add Upkeep Condition Data Serializer.
 */
public class AddUpkeepConditionDataSerializer extends AbstractCommandDataSerializer<IUpkeepCondition> implements IAddUpkeepConditionDataSerializer {

	/**
	 * Creates a new Add Upkeep Condition Data Serializer.
	 */
	public AddUpkeepConditionDataSerializer() {
		super(AddUpkeepCondition.COMMAND_ID);
	}

	@Override
	public IUpkeepCondition readReturnValue(IDataReader reader) throws IOException {
		IUpkeepCondition value = new UpkeepConditionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IUpkeepCondition value) throws IOException {
		new UpkeepConditionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IUpkeepCondition> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int upkeepId = reader.readInt();
		UpkeepConditionType type = reader.readEnum(UpkeepConditionType.class, true);
		int conditionId = reader.readInt();
		long conditionAmount = reader.readLong();

		// Create object
		AddUpkeepCondition object = new AddUpkeepCondition();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setUpkeepId(upkeepId);
		object.setType(type);
		object.setConditionId(conditionId);
		object.setConditionAmount(conditionAmount);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IUpkeepCondition> argument) throws IOException {
		AddUpkeepCondition object = (AddUpkeepCondition) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int upkeepId = object.getUpkeepId();
		UpkeepConditionType type = object.getType();
		int conditionId = object.getConditionId();
		long conditionAmount = object.getConditionAmount();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(upkeepId);
		writer.writeEnum(type, true);
		writer.writeInt(conditionId);
		writer.writeLong(conditionAmount);
	}

	@Override
	public IExecutableCommandResponse<IUpkeepCondition> newResponse() {
		return new AddUpkeepConditionResponse();
	}
}
